package com.wn.class30.cotroller.weixin;

import com.wn.class30.entity.UserAddress;
import com.wn.class30.service.UserAddressService1;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.UseridUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@Api(value = "用户地址操作",tags = "用户地址操作")
@RequestMapping(value = "/weixin")
@Validated
public class WUserAddressController {
    @Resource
    private UseridUtils useridUtils;

    @Resource
    private UserAddressService1 userAddressService;
    @GetMapping(value = "/queryUserAddress")
    @ApiOperation(value = "查询该用户的所有地址",notes = "查询该用户的所有地址")
    public Result queryUserAdress(HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid==null){
          return  new Result("200","success",null,null);
        }else{
            List<UserAddress> userAddresses = userAddressService.queryAddressByUserid(userid);
            return  new Result("200","success",userid,userAddresses);
        }
    }
    @ApiOperation(value = "更新用户的默认地址",notes = "更新用户的默认地址")
    @GetMapping("/updateDefaultAddress/{id}")
    public Result updateDefaultAddress(@PathVariable @NotNull Integer id, HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid==null){
            return  new Result("200","success",null,null);
        }else{
            userAddressService.updateDefaultAddressByUserId(userid,id);
            return  new Result("200","success","success",null);
        }
    }
    @GetMapping("/queryAddressByid/{id}")
    @ApiOperation(value = "更改地址详细信息",notes = "更改地址详细信息")
    public Result queryAddressByid(@PathVariable @NotNull Integer id, HttpServletRequest req)throws Exception{
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid==null){
            return  new Result("200","success",null,null);
        }else{
            UserAddress userAddress = userAddressService.queryAddressByAddressid(id);
            return  new Result("200","success",userAddress,null);
        }
    }
    @GetMapping("/insertDizhi")
    @ApiOperation(value = "新增地址信息",notes = "新增地址信息")
    public Result insertDizhi(@Valid @NotNull @NotBlank String name,@Valid @NotNull @NotBlank String tel,@Valid @NotNull @NotBlank String addr,HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid==null){
            return  new Result("200","success",null,null);
        }else{
            UserAddress userAddress1 = new UserAddress();
            userAddress1.setUserId(userid);
            userAddress1.setName(name);
            userAddress1.setTel(tel);
            userAddress1.setAddr(addr);
            userAddress1.setIsdefault(0);
             userAddressService.insertAddress(userAddress1);
            return  new Result("200","success",userid,null);
        }
    }


    @GetMapping("/updateDizhi")
    @ApiOperation(value = "修改地址信息",notes = "修改地址信息")
    public Result updateDizhi(@Valid @NotNull @NotBlank String name, @Valid @NotNull @NotBlank String tel, @Valid @NotNull @NotBlank String addr,@Valid @NotNull Integer id, HttpServletRequest req) throws Exception {
        Integer userid = useridUtils.queryUseridByOptenid(req);
        if(userid==null){
            return  new Result("200","success",null,null);
        }else{
            UserAddress userAddress1 = new UserAddress();
            userAddress1.setUserAddressId(id);
            userAddress1.setUserId(userid);
            userAddress1.setName(name);
            userAddress1.setTel(tel);
            userAddress1.setAddr(addr);
            userAddressService.updateAddress(userAddress1);
            return  new Result("200","success",userid,null);
        }
    }
}
